<template>
  <div class="course-user-page">
    <div class="head">浪屿帖子</div>
    <div class="select-button">
      <el-row>
        <el-col :span="12">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input v-model="inputContent" placeholder="请输入帖子内容关键词" @change="pageUsers" ></el-input>
            </el-col>
            <el-col :span="12">
              <el-input v-model="inputUserId" placeholder="请输入发帖人ID"  @change="pageUsers"></el-input>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="4" :offset="1">
          <el-select v-model="inputType" placeholder="帖子类型">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3" :offset="1">
          <el-button  type="primary" icon="el-icon-search" @click="pagePosts()">查询</el-button>
        </el-col>
        <el-col :span="3">
          <el-button  type="primary" icon="el-icon-search" @click="clear()">清空</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="student-list" style="display: flex;flex-direction: column;align-items: center">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="selection" width="55"  align="center">
        </el-table-column>
        <el-table-column fixed prop="id" label="ID" min-width="50%" align="center">
        </el-table-column>
        <el-table-column prop="nickname" label="发帖人昵称" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="type" label="类型" min-width="40%" align="center" >
          <template slot-scope="scope">
            <span v-if="scope.row.type=='0'">共创记忆</span>
            <span v-else-if="scope.row.type=='1'">共创文章</span>
            <span v-else-if="scope.row.type=='2'">共创帖子</span>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="visibility" label="可见等级" min-width="40%" align="center" >
          <template slot-scope="scope">
            <div>{{visibilityTypes[scope.row.visibility]}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="content" label="内容" min-width="40%" align="center" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <div v-html="scope.row.content" ></div>
          </template>
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞量" min-width="40%" align="center" >
        </el-table-column>
        <el-table-column prop="gmtCreate" label="发布时间" min-width="50%" align="center" >
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="250">
          <template slot-scope="scope">
            <el-button type="text" size="medium" @click="showVisibilityModel(scope.row.id,scope.$index)">修改可视权限</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="block" style="margin-top: 20px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[8, 10, 20, 40]"
          :page-size="8"
          layout="total, sizes, prev, pager, next, jumper"
          :total="parseInt(total)">
        </el-pagination>
      </div>
    </div>

    <el-dialog title="操作" show-close :visible.sync="postBindDialogVisible">
      <el-form label-width="120px">
        <el-form-item label="可视权限">
          <el-select v-model="visibilityType" filterable style="min-width: 450px">
            <el-option
                v-for="(item,index) in visibilityTypes"
                :key="item"
                :label="item"
                :value="index">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="postBindDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="upgrade">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "IslandManageView",
  data() {
    return {
      total: 0,
      currentSize: 8,
      currentPage: 1,
      visibilityType:0,
      visibilityTypes:["仅自己可见","仅关注可见","全部可见"],

      postBindDialogVisible: false,

      inputReplay:"",
      inputType: "",
      inputIslandId: "",
      inputContent: '',
      inputUserId: '',
      inputVisibility: "",
      debounceClock: null,

      postId : "",

      tableData: [],
      options: [{
        value: 0,
        label: '共创记忆'
      }, {
        value: 1,
        label: '共创文章'
      },{
        value: 2,
        label: '共创帖子'
      }]
    };
  },
  mounted() {
    this.islandId = this.$route.params.id
    this.postId = this.$route.query.linkId
    this.auditId = this.$route.query.auditId
    this.pagePosts();
  },
  methods: {
    pagePosts() {
      this.$api.pageIslandPosts({
        pageSize: this.currentSize,
        pageNow: this.currentPage,
        islandId: this.$route.params.id,
        type: this.inputType || '',
        content: this.inputContent || '',
        userId:this.inputUserId || '',
        id : this.postId || ''
      })
        .then((res) => {
          let list = res.data.rows;
          this.total = res.data.totalPage
          console.log(this.total)
          this.tableData = list
        });
    },
    handleSizeChange(val) {
      this.currentSize = parseInt(val);
      this.pagePosts();
    },
    handleCurrentChange(val) {
      this.currentPage = parseInt(val);
      this.pagePosts();
    },

    showVisibilityModel(postId,index) {
      this.currentBindPostId = postId
      this.postBindDialogVisible = true
      this.visibilityType = this.tableData[index].visibility
    },

    upgrade() {
      this.$api.upgrade({
        auditId: this.auditId,
        linkId : this.currentBindPostId,
        visibility: this.visibilityType,
        type : 4,// 对于后端AuditContentTypeEnum枚举值
      }).then(res => {
        this.postBindDialogVisible = false
        this.pagePosts()
      })
    },

    clear(){
      this.inputContent = ''
      this.inputUserId = ''
      this.inputType = ''
      this.pagePosts();
    }
  },
};
</script>

<style scoped>
.course-user-page{
  margin-top: 20px;
  width: 100%;
}


.head {
  padding-bottom: 10px;
}

.select-button {
  padding: 10px 0;
}

.student-list {
  width: 100%;
  padding: 10px 0;
  display: flex;
}
</style>
